package com.whfc.ms.api.base.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.UploadImgDTO;
import com.whfc.fuum.dto.AppProjectDTO;
import com.whfc.fuum.dto.AppProjectDetailDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.AppProjectParam;
import com.whfc.ms.api.base.service.MsAppProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/ms/api/project")
public class AppProjectController extends BaseController {

    @Autowired
    private MsAppProjectService msAppProjectService;

    /**
     * 获取项目列表
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("当前操作人：{},获取项目列表(分页)，page:{},pageSize:{},keyword:{}", user.getUsername(), pageNum, pageSize, keyword);
        PageData<AppProjectDTO> pageData = msAppProjectService.list(user, pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);

    }


    /**
     * 获取项目列表
     *
     * @param user
     * @return
     */
    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        logger.info("当前操作人：{},获取项目列表(不分页)", user.getUsername());
        ListData<AppProjectDTO> list = msAppProjectService.list(user);
        return ResultUtil.success(list);

    }

    /**
     * 根据项目id获得项目详情
     *
     * @param user
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result detail(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("id") Integer id) {
        logger.info("根据项目id获得项目详情, loginer：{},id:{}", user.getUsername(), id);
        AppProjectDetailDTO appProjectDetailDTO = msAppProjectService.detail(id);
        return ResultUtil.success(appProjectDetailDTO);
    }


    /**
     * 编辑项目的基本信息
     *
     * @param user
     * @param appProjectParam
     * @return
     */
    @RequiresPermissions("prj/list/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody AppProjectParam appProjectParam) {
        logger.info("当前操作人：{},增加项目的基本信息，{}", user.getUsername(), appProjectParam.toString());
        msAppProjectService.edit(appProjectParam);
        return ResultUtil.success();
    }

    /**
     * 上传项目宣传图片到服务器
     *
     * @param user
     * @param file
     * @return
     * @throws IOException
     */
    @RequiresPermissions("prj/list")
    @PostMapping("/uploadImg")
    public Result uploadImg(@SessionAttribute(SessionAttr.MS_USER) SysUser user, MultipartFile file) throws IOException {
        logger.info("当前操作人：{}，上传项目宣传图片到服务器", user.getUsername());
        UploadImgDTO uploadImgDTO = msAppProjectService.uploadImg(file.getInputStream());
        return ResultUtil.success(uploadImgDTO);
    }
}
